{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <link href="{% static 'dist/css/bootstrap.css' %}" rel="stylesheet">
    <script src="{% static 'app01/js/jQuery3.6.0.js' %}"></script>
    <style>
        .account {
            width: 400px;
            margin: 0px auto;
        }
    </style>
</head>
<body>
<div class="account">
    <h1 style="text-align: center;">注册</h1>
    <form class="horizontal" method="post" novalidate>
        {% csrf_token %}
        {% for field in form %}
            {% if field.name == 'code' %}
                <div class="form-group">
                    <label for="{{ field.auto_id }}">{{ field.label }}</label>
                    <div class="clearfix">
                        <div class="col-md-6" style="padding-left: 0px;">{{ field }}</div>
                        <div class="col-md-6">
                            <input type="button" class="btn btn-primary reg_btn" value="点击获取验证码">
                            <span class="pull-right" style="color:firebrick;">{{ field.errors.0 }}</span>
                        </div>
                    </div>
                </div>
            {% else %}
                <div class="form-group">
                    <label for="{{ field.auto_id }}">{{ field.label }}</label>
                    {{ field }}<span class="pull-right" style="color:firebrick;">{{ field.errors.0 }}</span>
                </div>
            {% endif %}
        {% endfor %}
        <button type="submit" class="btn btn-primary">注册</button>
    </form>
</div>
<script>
    $('.reg_btn').click(function () {
        $.ajax({
            url: '{% url 'sms' %}',
            type: 'post',
            data: {
                csrfmiddlewaretoken: $("[name='csrfmiddlewaretoken']").val(),
                mobile_phone: $('#id_mobile_phone').val(),
                tpl: 'register',
            },
            success: function (data) {
                if (data.msg) {
                    $('#id_mobile_phone').next().html(data.msg).css({'color': 'red'});
                    setTimeout(function () {
                        $('#id_mobile_phone').next().html('');
                    }, 2000)
                } else {
                    $('.reg_btn').val('60')
                }
            }
        })
    })
</script>
</body>
</html>